/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */
package de.kieselbach.swing.validation

abstract class ValidationComponent {

    def validator
    def component
    def checkers = [] as LinkedList
    def valid = true

    ValidationComponent(def val, def comp, def checker) {

        validator = val
        component = comp
        checkers << checker
    }

    def addChecker(def checker) {

        if(checker != null) {
            checkers << checker

            if(valid) {
                def v = checker(component)

                if(v != valid) {
                    valid = v
                    markComponent()
                }
            }
        }

        valid
    }

    def check() {

        def v = true
        checkers.each() {c ->
            if(v) {
                v = c(component)
            }
        }

        if(v != valid) {
            valid = v
            markComponent()
            validator.componentChanged(this)
        }

        valid
    }

    // ------------------------------------------------------------

    abstract void markComponent()
}